﻿//  Symplectic Spark
//  Copyright 2010 Symplectic Ltd
//  Created by Martyn Whitwell (martyn@symplectic.co.uk)

//  This file is part of Spark.

//  Spark is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.

//  Spark is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.

//  You should have received a copy of the GNU General Public License
//  along with Spark.  If not, see <http://www.gnu.org/licenses/>.

// $URL: https://symplectic-spark.googlecode.com/svn/trunk/website/App_Code/ElementType.cs $
// $LastChangedDate: 2010-03-04 16:37:03 +0000 (Thu, 04 Mar 2010) $
// $LastChangedRevision: 21 $
// $LastChangedBy: martyn@symplectic.co.uk $

using System;
using System.Xml.Linq;
using System.Text.RegularExpressions;

/// <summary>
/// Summary description for ElementType
/// </summary>

namespace Symplectic.Spark
{
    public class ElementType : BaseType
    {
        public int Id { get; private set; }
        public string Path { get; private set; }
        public string ApiCategory { get; private set; }
        public string ApiCategories { get; private set; }

        public ElementTypeTemplate Item { get; private set; }
        public ElementTypeTemplate Index { get; private set; }
        public Regex Matcher { get; private set; }


        public ElementType(XElement elementType)
        {
            try
            {
                Id = readAttributeInteger(elementType, "id");
                Name = readAttributeString(elementType, "name");
                Path = readAttributeString(elementType, "path");
                ApiCategories = readAttributeString(elementType, "api-categories");
                ApiCategory = readAttributeString(elementType, "api-category");

                if (elementType.Element("item") != null)
                    Item = new ElementTypeTemplate(elementType.Element("item"));

                if (elementType.Element("index") != null)
                    Index = new ElementTypeTemplate(elementType.Element("index"));
            }
            catch (Exception ex)
            {
                throw new Exception("Failed to Parse xml component " + elementType.ToString(), ex);
            }

            if (Path != null)
            {
                if (Path == "/")
                {
                    if (Global.ApplicationPathVirtual == "/")
                        Matcher = new Regex(@"^/[^/]*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                    else
                        Matcher = new Regex("^" + Global.ApplicationPathVirtual + @"/[^/]*$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
                }
                else
                    Matcher = new Regex(@"/" + Path + "/(?<name>[^/]+)(/((?<iscurrent>[^/]*$)|(?<othercurrent>.*$)))?$", RegexOptions.IgnoreCase | RegexOptions.Compiled);
            }
        }
    }
}
